import 'package:date_format/date_format.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';

class ItemMsg_V extends StatelessWidget {

  String icon;
  String nickname;
  String content;
  String img;
  String video;
  var time;
  int state;
  String formarttime;
  bool show;
  ItemMsg_V({
    this.icon,
    this.video,
    this.img,
    this.nickname,
    this.content,
    this.time,
    this.state,
  });


  @override
  Widget build(BuildContext context) {
    if(state == 0){
      show = false;
    }else{
      show = true;
    }

    return Container(
        padding: EdgeInsets.only(top: 18),
        child: Column(
          children: <Widget>[

            Row(
              children: <Widget>[
                iconWidget(),
                Expanded(
                  child: Container(
                    margin: EdgeInsets.only(left: 15),
                    child: Column(
                      children: <Widget>[
                        Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: <Widget>[
                            Expanded(
                              child: Text(nickname,
                                style: TextStyle(
                                    color: Colors.white,
                                    fontWeight: FontWeight.w600,
                                    fontSize: 16
                                ),),
                            ),
                            Container(
                                child:Visibility(
                                  visible: show,
                                  child: ClipOval(
                                    child: Container(
                                        width: 18,
                                        height: 18,
                                        color: Color(0xffFFFB01),
                                        child: Center(
                                          child: Text('$state',
                                            style: TextStyle(
                                                color: Colors.black,
                                                fontSize: 11
                                            ),),
                                        )
                                    ),
                                  ),)
                            ),
                          ],
                        ),
                        Container(
                          margin: EdgeInsets.only(top: 10),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: <Widget>[
                              Expanded(
                                child: Stack(
                                  children: [
                                    Visibility(
                                      visible: img.isNotEmpty,
                                      child: Row(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: <Widget>[
                                          Image.asset(
                                            'assets/image/pic.png',
                                            height: 20,
                                            width: 20,
                                          )
                                        ],
                                      ),
                                    ),
                                    Visibility(
                                        visible: video.isNotEmpty,
                                        child: Row(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Image.asset(
                                              'assets/image/send_mv.png',
                                              height: 20,
                                              width: 20,
                                            )
                                          ],
                                        )
                                    ),
                                    Visibility(
                                      visible: content.isNotEmpty,
                                      child: Text(
                                        content,
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                        softWrap: false,
                                        style: TextStyle(
                                          color: Colors.grey,
                                          fontSize: 13,
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                              Text(getTime(context),
                                style: TextStyle(
                                    color: Colors.grey,
                                    fontSize: 12
                                ),),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ],
        )
    );
  }

  String getTime(BuildContext context) {
    //当前时间
    var currnettime = new DateTime.now().millisecondsSinceEpoch;
    var temptime = currnettime - time;
    int day = 24 * 60 * 60 * 1000;
    String formarttime;
    //小于1天
    if (temptime < day) {
      int hour = 60 * 60 * 1000;
      int ago = temptime ~/ hour;
      if(ago >0){
        formarttime = 'hours ago';
      }else{
        formarttime = formatDate(DateTime.fromMillisecondsSinceEpoch(time), [ 'H',':','nn']);
      }
    } else {
      int ago = temptime ~/ day;
      formarttime = '$ago days ago';
    }
    return formarttime;
  }


  Widget iconWidget(){
    if(icon != '' && icon != null) {
      return new Stack(
        children: <Widget>[
          ClipOval(
            child: CachedNetworkImage(
              placeholder: (context, url) => Container(
                height: 52,
                width: 52,
                color: Color(0xff272443),
                child: Center(
                  child: Image.asset(
                    'assets/image/space_bg.png',
                    width: 25,
                    height: 25,
                  ),
                ),
              ),
              errorWidget: (context, url,error) => Container(
                height: 52,
                width: 52,
                color: Color(0xff272443),
                child: Center(
                  child: Image.asset(
                    'assets/image/space_bg.png',
                    width: 25,
                    height: 25,
                  ),
                ),
              ),
              imageUrl:icon,
              width: 52,
              height: 52,
              filterQuality: FilterQuality.none,
              fit: BoxFit.cover,),
          ),
//        online(),
        ],
      );
    } else {
      return new Stack(
        children: <Widget>[
          ClipOval(
            child: Container(
              height: 52,
              width: 52,
              color: Color(0xff272443),
              child: Center(
                child: Image.asset(
                  'assets/image/space_bg.png',
                  width: 25,
                  height: 25,
                ),
              ),
            ),
          ),
        ],
      );
    }
  }


  Widget typeContent(BuildContext context) {
    return Stack(
      children: [
        Visibility(
          visible: img.isNotEmpty,
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Image.asset(
                'assets/image/pic.png',
                height: 20,
                width: 20,
              )
            ],
          ),
        ),
        Visibility(
          visible: video.isNotEmpty,
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Image.asset(
                'assets/image/send_mv.png',
                height: 20,
                width: 20,
              )
            ],
          )
        ),
        Visibility(
          visible: content.isNotEmpty,
          child: Text(
            content,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            softWrap: false,
            style: TextStyle(
              color: Colors.grey,
              fontSize: 13,
            ),
          ),
        ),
      ],
    );
    // if (img.isNotEmpty) {
    //   return Row(
    //     crossAxisAlignment: CrossAxisAlignment.start,
    //     children: <Widget>[
    //       Image.asset(
    //         'assets/image/pic.png',
    //         height: 20,
    //         width: 20,
    //       )
    //     ],
    //   );
    // } else if (voice.isNotEmpty) {
    //   return Row(
    //     crossAxisAlignment: CrossAxisAlignment.start,
    //     children: <Widget>[
    //       Icon(CupertinoIcons.volume_up,color: Colors.grey,size: 18,)
    //     ],
    //   );
    // } else if (gift.isNotEmpty) {
    //   return Row(
    //     crossAxisAlignment: CrossAxisAlignment.start,
    //     children: [
    //       Image.asset(
    //         'assets/image/liwu.png',
    //         height: 18,
    //         width: 18,
    //       )
    //     ],
    //   );
    // } else if (video.isNotEmpty) {
    //   return Row(
    //     crossAxisAlignment: CrossAxisAlignment.start,
    //     children: [
    //       Image.asset(
    //         'assets/image/send_mv.png',
    //         height: 20,
    //         width: 20,
    //       )
    //     ],
    //   );
    // } else if(yuying.isNotEmpty) {
    //   return Text(
    //     content,
    //     maxLines: 1,
    //     overflow: TextOverflow.ellipsis,
    //     softWrap: false,
    //     style: TextStyle(
    //       color: Colors.grey,
    //       fontSize: 13,
    //     ),
    //   );
    // } else {
    //   return Text(
    //     content,
    //     maxLines: 1,
    //     overflow: TextOverflow.ellipsis,
    //     softWrap: false,
    //     style: TextStyle(
    //       color: Colors.grey,
    //       fontSize: 13,
    //     ),
    //   );
    // }
  }
}